﻿Ext.define('Html5App.controller.LoginController', {
	extend : 'Ext.app.Controller',
	config : {
		models : [],
		views : [],
		refs : {
			login : 'login',
			userLoginAction : 'button[action=userLogin]'
		},
		control : {
			login : {
				activate : function(newActiveItem, t, oldActiveItem, eOpts) {
					if(navigator.appinfo && typeof(navigator.appinfo.appversion) != "undefined"){
						var resultCallback = function(result) {
							Ext.getCmp('app_version').setHtml(result.appversion);
						};
						navigator.appinfo.appversion(resultCallback);
					}
					config.database.transaction(
						function (tx) {
                    		tx.executeSql(
				            	"select * from LastLoginSalesMan",
				            	[],
				            	function (tx, result) {
				            		if(Number(result.rows.length) > 0){
				            			var obj = result.rows.item(0);
				            			Ext.getCmp('user_name').setValue(obj['SalesCode'])
				            		}
				            	},
				            	function (tx, error) {}
				            );
						}
					);
				}
			},
			// 登录
			userLoginAction : {
				tap : function(button, e, eOpts) {
					var user_name = Ext.getCmp('user_name').getValue();
					if(user_name == ""){
                    	util.showMessageMask('請輸入編號','warn');
                    	return;
                    }
                    var pass_word = Ext.getCmp('pass_word').getValue();
                    if(pass_word == ""){
                    	util.showMessageMask('請輸入密碼','warn');
                    	return;
                    }
                    config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select * from SalesMan where SalesCode='"+user_name+"' and Passwd='"+pass_word+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				var obj = r.rows.item(0);
				        				config.current_user = {
		                        			user_name : obj['SalesCode'],
		                        			sales_name : obj['SalesName']
		                        		};
		                        		config.database.transaction(
											function (tx) {
				                        		tx.executeSql(
									            	"select * from LastLoginSalesMan where SalesCode='"+user_name+"'",
									            	[],
									            	function (tx, result) {
									            		if(Number(result.rows.length) > 0){
									            			config.redirec.redirectTo('redirec/home');
									            		}else{
									            			navigator.notification.confirm(
																'是否清除原用戶數據及資料并下載當前用戶數據及資料？',
																function(btnId){
																	if(btnId==2){
																		config.database.transaction(
																			function (tx) {
																				//清空LastLoginSalesMan表
																				tx.executeSql("delete from LastLoginSalesMan",
																					[],
																					function (tx, r) {},
																					function (tx, e) {}
																				);
										                        				//写入LastLoginSalesMan表
										                        				tx.executeSql("insert into LastLoginSalesMan(SalesCode,Passwd,SalesName) values ('"+user_name+"','"+pass_word+"','"+config.current_user.sales_name+"')",
																					[],
																					function (tx, r) {
																						custom.downloadDataBySalesCode(user_name,true);
																					},
																					function (tx, e) {}
																				);
																			}
																		);
																	}
														        }, '溫馨提示', ['取消','確定']
															);
									            		}
									            	},
									            	function (tx, error) {}
									            );
											}
										);
				        			}else{
				        				util.showLoadMask('登入中...');
					                	Ext.data.JsonP.request({
					                        url: config.global_url+'/userLogin.html',
					                        callbackKey: 'callback',
					                        params: {
					                        	SalesCode :  user_name,
					                        	Passwd : pass_word
					                        },
					                        success: function(result, request) {
					                        	util.hideLoadMask();
					                        	if (result.result) {
					                        		config.current_user = {
					                        			user_name : result.data.SalesCode,
					                        			sales_name : result.data.SalesName
					                        		};
					                        		config.database.transaction(
														function (tx) {
							                        		tx.executeSql(
												            	"select * from LastLoginSalesMan where SalesCode='"+user_name+"'",
												            	[],
												            	function (tx, result) {
												            		if(Number(result.rows.length) > 0){
												            			config.redirec.redirectTo('redirec/home');
												            		}else{
												            			navigator.notification.confirm(
																			'是否清除原用戶數據及資料并下載當前用戶數據及資料？',
																			function(btnId){
																				if(btnId==2){
																					config.database.transaction(
																						function (tx) {
																							//清空LastLoginSalesMan表
																							tx.executeSql("delete from LastLoginSalesMan",
																								[],
																								function (tx, r) {},
																								function (tx, e) {}
																							);
													                        				//写入LastLoginSalesMan表
													                        				tx.executeSql("insert into LastLoginSalesMan(SalesCode,Passwd,SalesName) values ('"+user_name+"','"+pass_word+"','"+config.current_user.sales_name+"')",
																								[],
																								function (tx, r) {
																									custom.downloadDataBySalesCode(user_name,true);
																								},
																								function (tx, e) {}
																							);
																						}
																					);
																				}
																	        }, '溫馨提示', ['取消','確定']
																		);
												            		}
												            	},
												            	function (tx, error) {}
												            );
														}
													);
					                        	}else{
					                        		util.showMessageMask(result.msg,'warn-max');
					                        	}
					                        },
					                        failure: function () {
					                        	util.showMessageMask('連接服務器失敗，請稍後再試','error-max');
					                        }
					                    });
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
				}
			}
		}
	}
});